<template>
	<el-container>
		<el-header>
			<div class="left-panel">
				<el-button type="primary" v-auth="'SysUser.Create'" icon="el-icon-plus" @click="add"></el-button>
				<el-button type="danger" v-auth="'SysUser.Delete'" plain icon="el-icon-delete" :disabled="selection.length==0" @click="batch_del"></el-button>
			</div>
			<div class="right-panel">
				<div class="right-panel-search">
					<el-input v-model="search.id" placeholder="ID" clearable></el-input>
					<el-input v-model="search.username" placeholder="账号" clearable></el-input>
					<el-input v-model="search.name" placeholder="姓名" clearable></el-input>
					<el-input v-model="search.mobile" placeholder="手机" clearable></el-input>
					<el-input v-model="search.mail" placeholder="邮箱" clearable></el-input>
					<el-input v-model="search.roleName" placeholder="角色" clearable></el-input>
					<el-button type="primary" icon="el-icon-search" @click="upsearch"></el-button>
					<el-button type="warning" icon="el-icon-circle-close" @click="upclear"></el-button>
				</div>
			</div>
		</el-header>
		<el-main class="nopadding">
			<scTable ref="table" :apiObj="apiObj" row-key="id" @selection-change="selectionChange" stripe :params="search">
				<el-table-column type="selection" width="50"></el-table-column>
                <el-table-column label="账号" prop="username" min-width="120"></el-table-column>
                <el-table-column label="姓名" prop="name" min-width="120"></el-table-column>
                <el-table-column label="邮箱" prop="mail" min-width="180"></el-table-column>
                <el-table-column label="手机号码" prop="mobile" min-width="110"></el-table-column>
                <el-table-column label="电话号码" prop="phone" min-width="100"></el-table-column>
				<el-table-column label="分机号码" prop="phoneExt" min-width="80"></el-table-column>
                <el-table-column label="启用状态" prop="isDisabled">					
					<template #default="scope">			
						<span v-if="scope.row.isDisabled==1" plain size="small" text>
							<sc-status-indicator type="danger"></sc-status-indicator> 禁用
						</span>			
						<span v-if="scope.row.isDisabled!=1" plain size="small" text>
							<sc-status-indicator type="success"></sc-status-indicator> 启用
						</span>
					</template>
				</el-table-column>
                <el-table-column label="邮箱验证" prop="isMail">
					<template #default="scope">						
						<span v-if="scope.row.isMail==1" plain size="small" text>
							<sc-status-indicator type="success"></sc-status-indicator> 已验证
						</span>
						<span v-if="scope.row.isMail!=1" plain size="small" text>
							<sc-status-indicator type="info"></sc-status-indicator> 未验证
						</span>
					</template>
				</el-table-column>
                <el-table-column label="手机验证" prop="isMobile">
					<template #default="scope">						
						<span v-if="scope.row.isMobile==1" plain size="small" text>
							<sc-status-indicator type="success"></sc-status-indicator> 已验证
						</span>
						<span v-if="scope.row.isMobile!=1" plain size="small" text>
							<sc-status-indicator type="info"></sc-status-indicator> 未验证
						</span>
					</template>
				</el-table-column>
                <el-table-column label="注册审批" prop="regApproval">
					<template #default="scope">
						<span v-if="scope.row.regApproval==0" plain size="small" text>
							<sc-status-indicator type="warning"></sc-status-indicator> 待审批
						</span>
						<span v-if="scope.row.regApproval==1" plain size="small" text>
							<sc-status-indicator type="success"></sc-status-indicator> 已批准
						</span>
						<span v-if="scope.row.regApproval==2" plain size="small" text>
							<sc-status-indicator type="danger"></sc-status-indicator> 已拒绝
						</span>
					</template>
				</el-table-column>
                <el-table-column label="登录次数" prop="loginCount"></el-table-column>
                <el-table-column label="最后登录IP" prop="loginIp" width="90"></el-table-column>
                <!-- <el-table-column label="头像" prop="avatar"></el-table-column> -->
                <!-- <el-table-column label="所属部门ID" prop="deptId"></el-table-column> -->
                <!-- <el-table-column label="岗位ID" prop="postId"></el-table-column> -->
                <el-table-column :label="`最后登录(`+this.$CONFIG.DATE_TIME_ZONE_TEXT+`)`" prop="loginTime" width="150">
					<template #default="scope">
						{{ this.$TOOL.dateFormat(scope.row.loginTime) }}
					</template>
				</el-table-column>
                <el-table-column :label="`过期时间(`+this.$CONFIG.DATE_TIME_ZONE_TEXT+`)`" prop="expiredTime" width="150">
					<template #default="scope">
						{{ this.$TOOL.dateFormat(scope.row.expiredTime) }}
					</template>
				</el-table-column>
                <el-table-column :label="`创建时间(`+this.$CONFIG.DATE_TIME_ZONE_TEXT+`)`" prop="createTime" width="150">
					<template #default="scope">
						{{ this.$TOOL.dateFormat(scope.row.createTime) }}
					</template>
				</el-table-column>
                <!-- <el-table-column label="修改时间" prop="updateTime"></el-table-column> -->
                <el-table-column label="备注" prop="remark" show-overflow-tooltip></el-table-column>
				<el-table-column label="操作" fixed="right" align="right" width="170">
					<template #default="scope">
						<el-button-group>
							<el-button text type="primary" size="small" @click="table_show(scope.row, scope.$index)">查看</el-button>
							<el-button text v-auth="'SysUser.Update'" type="primary" size="small" @click="table_edit(scope.row, scope.$index)">编辑</el-button>
							<el-popconfirm title="请选择审批结果" 
							confirmButtonText="同意" cancelButtonText="拒绝"
							:disabled="scope.row.regApproval!=0"
							@confirm="table_approval(scope.row, 1)"
							@cancel="table_approval(scope.row, 0)"
							>
								<template #reference>
									<el-button text v-auth="'SysUser.Update'" type="primary" size="small" :disabled="scope.row.regApproval!=0">审批</el-button>
								</template>
							</el-popconfirm>
							<el-button text v-auth="'SysUser.Update'" type="primary" size="small" @click="password_show(scope.row, scope.$index)">密码重置</el-button>
							<el-button text v-auths="['SysUser.Update','SysRoleUser.Read']" type="primary" size="small" @click="roles_show(scope.row, scope.$index)">角色设置</el-button>
							<el-button text v-auth="'SysUser.Update'" type="primary" size="small" @click="disable_show(scope.row, scope.$index)">启用/禁用</el-button>
							<el-popconfirm title="确定删除吗？" @confirm="table_del(scope.row, scope.$index)">
								<template #reference>
									<el-button v-auth="'SysUser.Delete'" text type="primary" size="small">删除</el-button>
								</template>
							</el-popconfirm>
						</el-button-group>
					</template>
				</el-table-column>
			</scTable>
		</el-main>
	</el-container>

	<save-dialog v-if="dialog.save" ref="saveDialog" @success="handleSaveSuccess" @closed="dialog.save=false"></save-dialog>
	<password-dialog v-if="dialog.password" ref="passwordDialog" @success="handlePasswordSuccess" @closed="dialog.password=false"></password-dialog>
	<disable-dialog v-if="dialog.disable" ref="disableDialog" @success="handleDisableSuccess" @closed="dialog.disable=false"></disable-dialog>
	<roles-dialog v-if="dialog.roles" ref="rolesDialog" @success="handleRoleSuccess" @closed="dialog.roles=false"></roles-dialog>

</template>

<script>
	import saveDialog from './save'
	import disableDialog from './disable'
	import passwordDialog from './password'
	import rolesDialog from './roles'
	import setting from './setting'

	export default {
		name: 'role',
		components: {
			saveDialog,
			passwordDialog,
			disableDialog,
			rolesDialog
		},
		data() {
			return {
				dialog: {
					save: false,
					password: false,
					disable: false,
					roles: false
				},
				apiObj: setting.api.list,
				selection: [],
				search: {
					id: this.$route.query.id || '',
					username: this.$route.query.usename || '',
					name: this.$route.query.name || '',
					mobile: this.$route.query.mobile || '',
					mail: this.$route.query.mail || '',
					roleName: this.$route.query.roleName || ''
				}
			}
		},
		methods: {
			//添加
			add(){
				this.dialog.save = true
				this.$nextTick(() => {
					this.$refs.saveDialog.open()
				})
			},
			//编辑
			table_edit(row){
				this.dialog.save = true
				this.$nextTick(() => {
					this.$refs.saveDialog.open('edit').setData(row)
				})
			},
			//查看
			table_show(row){
				this.dialog.save = true
				this.$nextTick(() => {
					this.$refs.saveDialog.open('show').setData(row)
				})
			},
			//删除
			table_del(row){
				this.table_del_ids(row.id)
			},
			//审批
			table_approval(row,val){
				this.table_approval_ids(row.id,val)				
			},
			async table_approval_ids(ids,val){
				const loading = this.$loading();
				var reqData = {
					ids: ids,
					msg: "",
					approval: val == 1 ? "Agree" : "Reject"
				}
				var res = await setting.api.approval.post(reqData);
				if(res.code == setting.successCode){
					this.$refs.table.refresh()
					this.$message.success("审批完成")
				}else{
					this.$alert(res.message, "提示", {type: 'error'})
				}
				loading.close();
			},
			async table_del_ids(ids){
				const loading = this.$loading();
				var reqData = {ids: ids}
				var res = await setting.api.del.post(reqData);
				if(res.code == setting.successCode){
					this.$refs.table.refresh()
					this.$message.success("成功删除"+ res.data +"条数据")
				}else{
					this.$alert(res.message, "提示", {type: 'error'})
				}
				loading.close();
			},
			//批量删除
			async batch_del(){
				this.$confirm('确定删除选中的 '+ this.selection.length +' 项吗？如果删除项中含有子集将会被一并删除', '提示', {
					type: 'warning'
				}).then(() => {
					const ids = this.selection.map(item=>item.id).join(',')
					this.table_del_ids(ids)
				}).catch(() => {

				})
			},
			//表格选择后回调事件
			selectionChange(selection){
				this.selection = selection;
			},
			//表格内开关
			changeSwitch(val, row){
				row.status = row.status == '1'?'0':'1'
				row.$switch_status = true;
				setTimeout(()=>{
					delete row.$switch_status;
					row.status = val;
					this.$message.success("操作成功")
				}, 500)
			},
			//搜索
			upsearch(){
				this.$refs.table.upData(this.search)
			},
			upclear(){
				this.search.id=''
				this.search.name=''
				this.search.username=''
				this.search.mobile=''
				this.search.mail=''
				this.search.roleName=''
				this.$refs.table.upData(this.search)
			},
			//本地更新数据
			handleSaveSuccess(data, mode){
				if(mode=='add'){
					this.$refs.table.refresh()
				}else if(mode=='edit'){
					this.$refs.table.refresh()
				}
			},
			//密码设置显示
			password_show(row){
				this.dialog.password = true
				this.$nextTick(() => {
					this.$refs.passwordDialog.open().setData(row)
				})
			},
			//密码重置完成
			handlePasswordSuccess() {
				this.$refs.table.refresh()
			},
			//启/禁用显示
			disable_show(row) {
				this.dialog.disable = true
				this.$nextTick(() => {
					this.$refs.disableDialog.open().setData(row)
				})
			},
			//启/禁用完成
			handleDisableSuccess(){
				this.$refs.table.refresh()
			},
			//用户角色设置显示
			roles_show(row) {
				this.dialog.roles = true
				this.$nextTick(() => {
					this.$refs.rolesDialog.open(row)
				})
			},
			//用户角色设置完成
			handleRoleSuccess(){
			}
		}
	}
</script>

<style>
</style>
